<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Inscripciones
 *
 * @author Virtualidad
 *
 * @export Modules
 * 
 */
class Inscripciones extends Module {

    public function index() {
        $codigoTipo = $this->request->get("CodigoTipo", 0);

        $this->assign("tiposPlanes", array(0 => "Pregrado", 1 => "Posgrado", 2 => "Diplomado"));
        $this->assign("codigoTipo", $codigoTipo);

        $this->assign("careers", $this->model->career->GetByTipoPrograma($codigoTipo, false));
        $this->assign("inscribirse", $this->route->action("Inscribirse"));

        $this->display("index");
    }

    public function Inscribirse() {
        $codigoPlan = $this->request->get("CodigoPlan", "");
        $nombrePlan = $this->request->get("NombrePlan", "");
        $codigoTipo = $this->request->get("CodigoTipo", "");

        if ($codigoPlan == "" || $nombrePlan == "" || $codigoTipo == "") {
            $this->index();
        } else {
            $this->assign("tiposPlanes", array(0 => "Pregrado", 1 => "Posgrado", 2 => "Diplomado"));
            $this->assign("codigoPlan", $codigoPlan);
            $this->assign("nombrePlan", $nombrePlan);
            $this->assign("codigoTipoActual", $codigoTipo);

            $this->assign("paises", $this->model->countries->GetAll());
            $this->assign("action", $this->route->action("Save"));
            $this->assign("return", $this->route->action("index"));
            $this->assign("GetProgramas", $this->route->action("GetProgramas"));

            $this->display();
        }
    }

    public function Save() {

        //index.php?option=com_acaweb&controller=Acaweb.Inscripciones&task=Save

        if ($this->request->submitted()) {
            Loader::using("ext::libs::createXFDF");
            
            $values = $this->request->post("Fields");
            $this->saveDataFile($values);

            $this->addMessage(new Message("Sus datos han sido guardados con &eacute;xito.", "Listo", Message::SUCCESS));

            $this->assign("link", $this->route->moduleAction("DescargarInscripcionPDF", "DescargarPDF", array("NumeroDocumento" => $values["NumerodeDocumento"],)));
            
            $this->display();
        } else {
            $this->index();
        }
    }

    private function saveDataFile($values) {
        $values["FechaRegistro"] = date("d/m/Y");

        $data = createXFDF("Formulario.pdf", $values, "ISO-8859-1");

        $fileName = md5($values["NumerodeDocumento"] . ModulesContext::instance()->getConfig()->get("periodoActual")) . ".xfdf";
        $path = JPATH_ROOT . DS . "tmp" . DS . "com_acaweb" . DS;

        $file = fopen($path . $fileName, "w");

        if ($file) {
            fwrite($file, $data, strlen($data));
        }

        fclose($file);
    }

    public function GetProgramas() {
        $TipoPrograma = $this->request->get("TipoPrograma");
        echo($this->model->career->GetByTipoPrograma($TipoPrograma));
    }

    public function GetMunicipio() {
        $Departamento = $this->request->get("CodigoDepartamento");
        echo($this->model->cities->GetByStates($Departamento));
    }

    public function GetDepartamento() {
        $Pais = $this->request->get("CodigoPais");
        echo($this->model->states->GetByPais($Pais));
    }

    private function processDetalleConcepto($detalleConcepto) {
        return $detalleConcepto + 3;
    }

}

?>
